package com.lhn.tankGame3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @author lhn
 * @date 2024-07-24 18:01
 * 坦克大战的绘图区域
 */

public class MyPanel extends JPanel implements KeyListener, Runnable {

    //我的坦克
    Hero play1Hero = null;
    Hero play2Hero = null;


    //定义敌人的坦克，放入Vector
    Vector<EnemyTank> enemyTanks = new Vector<>();
    int enemyTankSize = 3;

    public MyPanel(){
        //初始化

        play1Hero = new Hero(100, 600, 0);
        play2Hero = new Hero(400, 600, 0, 15);

        for (int i = 0; i < enemyTankSize; i++) {
            enemyTanks.add(new EnemyTank( (150 * ( i + 1) ), 0 , 2));
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);
        drawTank(play1Hero.getX(), play1Hero.getY(), g, play1Hero.getDirection(), 1);
        drawTank(play2Hero.getX(), play2Hero.getY(), g, play2Hero.getDirection(), 2);

        //画出子弹
        if ((play1Hero.shoot != null) && (play1Hero.shoot.isLive != false)) {
            g.setColor(Color.yellow);
            g.fill3DRect(play1Hero.shoot.getX(), play1Hero.shoot.getY(), 5, 5, false);
        }

        if ((play2Hero.shoot != null) && (play2Hero.shoot.isLive != false)) {
            g.setColor(Color.GREEN);
            g.fill3DRect(play2Hero.shoot.getX(), play2Hero.shoot.getY(), 5, 5, false);
        }

        EnemyTank enemyTank = null;
        for (int i = 0; i < enemyTankSize; i++) {
            enemyTank = enemyTanks.get(i);
            drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), 0);

        }

    }

    //画出坦克
    /**
    *
    * @author lihaonan
    * @date 2024/7/26 15:28
     * @param x 坦克的左上角x坐标
     * @param y 坦克的左上角y坐标
     * @param g 画笔
     * @param direction 坦克的方向（上下左右）
     * @param type  坦克类型
    */
    public void drawTank(int x, int y, Graphics g, int direction, int type) {

        //根据不同类型的坦克设置不同的颜色
        switch (
                type
        ) {
            case 0: //敌人使用的坦克
                g.setColor(Color.cyan);
                break;
            case 1: //玩家1
                g.setColor(Color.yellow);
                break;
            case 2:
                g.setColor(Color.GREEN);
                break;
        }

        //根据坦克的方向来绘制坦克
        switch (
                direction
        ) {
            case 0:
                //向上
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fill3DRect(x + 10, y + 20, 20, 20, false);
                g.drawLine(x + 20, y + 30, x + 20, y);
                break;
            case 1:
                //向右
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fill3DRect(x + 20, y + 10, 20, 20, false);
                g.drawLine(x + 30, y + 20, x + 60, y + 20);
                break;
            case 2:
                //向下
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fill3DRect(x + 10, y + 20, 20, 20, false);
                g.drawLine(x + 20, y + 30, x + 20, y + 60 );
                break;
            case 3:
                //向左
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fill3DRect(x + 20, y + 10, 20, 20, false);
                g.drawLine(x + 30, y + 20, x, y + 20);
                break;
            default:
                System.out.println("暂时无法处理");
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        //方向键
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            play2Hero.moveLeft();
        }
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            play2Hero.moveUp();
        }
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            play2Hero.moveDown();
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            play2Hero.moveRight();
        }
        if (e.getKeyCode() == KeyEvent.VK_COMMA) {
            play2Hero.shootEnemy();
        }

        //WASD
        switch (
                e.getKeyCode()
        ) {
            case 87:
//                System.out.println("上");
                play1Hero.moveUp();
                break;
            case 65:
//                System.out.println("左");
                play1Hero.moveLeft();
                break;
            case 83:
//                System.out.println("下");
                play1Hero.moveDown();
                break;
            case 68:
//                System.out.println("右");
                play1Hero.moveRight();
                break;
            case 74:
                //发射子弹
                play1Hero.shootEnemy();
                break;
        }

        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            this.repaint();
        }

    }
}
